package com.zhang.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.entity.User;
import com.zhang.exception.BusinessException;
import com.zhang.mapper.UserMapper;
import com.zhang.responce.ResultCode;
import com.zhang.service.UserService;
import com.zhang.utils.JWTUtils;
import com.zhang.utils.MD5Utils;
import com.zhang.vo.UserVO;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Zhang
 * @since 2022-03-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 功能描述: <br>
     * 〈分页查询〉
     * Constants.WRAPPER 表示wrapper对象
     *
     * @param userVO
     * @param page
     * @Param: [page 分页对象, queryWrapper 查询条件]
     * @Return: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.zhang.entity.User>
     * @Author: Mr.zhang
     * @Date: 2022/3/2 15:43
     */
    @Override
    public Page<UserVO> findUserPage(Page<UserVO> page, UserVO userVO) {
        List<UserVO> userList = this.baseMapper.findUserPage(page, userVO);
        page.setRecords(userList);
        return page;
    }

    /**
     * 功能描述: <br>
     * 〈修改状态，是禁用变成开发，是开发变成禁用〉
     *
     * @param id
     * @Param: [id]
     * @Return: boolean
     * @Author: Mr.zhang
     * @Date: 2022/3/6 2:50
     */
    @Override
    public boolean modifyStatus(Long id) {
        return this.baseMapper.modifyStatus(id) > 0;
    }

    /**
     * 功能描述: <br>
     * 〈导出用户信息〉
     *
     * @param response ：响应的处理器
     * @param users    : 要导出的用户集合
     * @Param: [response, tbUsers]
     * @Return: void
     * @Author: Mr.zhang
     * @Date: 2022/3/8 12:03
     */
    @Override
    public void exportUserExcel(HttpServletResponse response, List<User> users) throws IOException {
        //----- 写入excel文件
        String exportFileName = "TbUserInfo.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(response.getOutputStream(), User.class)
                .sheet("用户信息")
                //自动列宽
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(users);

    }

    /**
     * 功能描述: <br>
     * 〈实现用户名密码的登录〉
     *
     * @param username
     * @param password
     * @Param: [username, password]
     * @Return: java.lang.String
     * @Author: Mr.Nie
     * @Date: 2022/3/14 14:12
     */
    @Override
    public String login(String username, String password) {
        String token = null;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        List<User> users = this.baseMapper.selectList(wrapper);
        User user = users.get(0);
        if (user!=null){
            String userPassword = user.getPassword();
            String userSalt = user.getSalt();
            //对密码进行加密加盐处理
            String encryptionPassword = MD5Utils.md5Encryption(password, userSalt);
            if (userPassword.equals(encryptionPassword)) {
                token = JWTUtils.sign(username, encryptionPassword);
            }else {
                throw new BusinessException(ResultCode.USER_CREDENTIALS_ERROR.getCode(),
                        ResultCode.USER_CREDENTIALS_ERROR.getMessage());
            }
            return token;
        }else {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
    }

    /**
     * 功能描述: <br>
     * 〈 根据用户查询用户信息 〉
     *
     * @param username
     * @Param: [username]
     * @Return: com.zhang.entity.User
     * @Author: Mr.Zhang
     * @Date: 2022/3/17 8:55
     */
    @Override
    public User loadUserByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return this.getOne(wrapper);
    }
}
